#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int mySqrt(int x)
    {
        if (x < 1) return 0;
        long long left = 0, right = x;
        while (left < right)
        {
            long long mid = left + (right - left + 1) / 2;
            if (mid * mid <= x)
            {
                left = mid;
            }
            else if (mid * mid > x)
            {
                right = mid - 1;
            }
        }
        return left;

    }
};
/////////////////////////////
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target)
    {
        if (nums.size() == 0)
        {
            return { -1,-1 };
        }
        vector<int> ret;
        //找左端点
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else if (nums[mid] >= target)
            {
                right = mid;
            }
        }
        if (nums[left] != target)
        {
            return { -1,-1 };
        }
        else
        {
            ret.push_back(left);
        }
        //找右端点
        right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] <= target)
            {
                left = mid;
            }
            else if (nums[mid] > target)
            {
                right = mid - 1;
            }
        }
        if (nums[left] != target)
        {
            return { -1,-1 };
        }
        else
        {
            ret.push_back(left);
        }
        return ret;
    }
};
////////////////////////////////
class Solution {
public:
    int search(vector<int>& nums, int target)
    {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
            {
                right = mid - 1;
            }
            else if (nums[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                return mid;
            }
        }
        return -1;

    }
};